package com.webamc.controller;

import com.webamc.common.enums.Params;
import com.webamc.model.User;
import com.webamc.validator.IValidator;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Date: Mar 14, 2009
 * Time: 2:23:27 AM
 */
public abstract class AbstractAction extends MultiActionController {
    private String primaryView;
    private String errorView;

    private String beforeUpdateView;
    private String updateView;
    private String beforeCreateView;
    private String createView;

    private String beforeSearchView;
    private String searchView;

    private String listView;
    private String singleView;
    private Map<String, String> actionPermit;
    private IValidator validator;


    public IValidator getValidator() {
        return validator;
    }

    public void setValidator(IValidator validator) {
        this.validator = validator;
    }

    public void setPrimaryView(String primaryView) {
        this.primaryView = primaryView;
    }

    public String getPrimaryView() {
        return primaryView;
    }

    public String getErrorView() {
        return errorView;
    }

    public void setErrorView(String errorView) {
        this.errorView = errorView;
    }

    public String getBeforeUpdateView() {
        return beforeUpdateView;
    }

    public void setBeforeUpdateView(String beforeUpdateView) {
        this.beforeUpdateView = beforeUpdateView;
    }

    public String getUpdateView() {
        return updateView;
    }

    public void setUpdateView(String updateView) {
        this.updateView = updateView;
    }

    public String getBeforeCreateView() {
        return beforeCreateView;
    }

    public void setBeforeCreateView(String beforeCreateView) {
        this.beforeCreateView = beforeCreateView;
    }

    public String getCreateView() {
        return createView;
    }

    public void setCreateView(String createView) {
        this.createView = createView;
    }

    public String getBeforeSearchView() {
        return beforeSearchView;
    }

    public void setBeforeSearchView(String beforeSearchView) {
        this.beforeSearchView = beforeSearchView;
    }

    public String getSearchView() {
        return searchView;
    }

    public void setSearchView(String searchView) {
        this.searchView = searchView;
    }


    public String getSingleView() {
        return singleView;
    }

    public void setSingleView(String singleView) {
        this.singleView = singleView;
    }

    public String getListView() {
        return listView;
    }

    public void setListView(String listView) {
        this.listView = listView;
    }

    public Map<String, String> getActionPermit() {
        return actionPermit;
    }

    public void setActionPermit(Map<String, String> actionPermit) {
        this.actionPermit = actionPermit;
    }

    public static User getSessionUser(HttpServletRequest req) {
        return (User) req.getSession().getAttribute(Params.SESSIONUSER);
    }

    public static Map getParams(HttpServletRequest req) {
        return req.getParameterMap();
    }

    public boolean isAuthorized(User u, String action) {
        if (actionPermit != null && actionPermit.containsKey(action)) {
            String role = u.getCategory();
            String validRoles = actionPermit.get(action);
            return validRoles.matches(role); //TODO - optimize
        }

        return true;///NO rule found
    }

    public ModelAndView setMV(ModelAndView mv, String msg, String view) {
        mv.addObject(Params.MSGS, msg);
        mv.setViewName(view);
        return mv;
    }

    public ModelAndView setMV(ModelAndView mv, List<String> msg, String view) {
        mv.addObject(Params.MSGS, msg.toArray());
        mv.setViewName(view);
        return mv;
    }
}
